package photosalvage.rs;

import java.net.URI;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import photosalvage.PhotoSalvageConstants;
import photosalvage.entity.User;
import photosalvage.rs.top.TopPage;
import photosalvage.rs.users.Users;

public class WebUtils {

	public static Response redirect2LoginPage() {
		URI loginPageUri =
			UriBuilder.fromResource(Users.class)
			.path(Users.class, "loginPage").build();
		return Response.seeOther(loginPageUri).build();
	}

	public static Response redirect2TopPage() {
		URI topPageUri =
			UriBuilder.fromResource(TopPage.class).build();

		return Response.seeOther(topPageUri).build();
	}

	public static User getCurrentUser(HttpServletRequest request) {
		HttpSession session = request.getSession(false);
		if (session == null) {
			return null;
		}
		return (User) session.getAttribute(PhotoSalvageConstants.SESSION_KEY_USER);
	}
}
